/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.storage.entity.Route;
import com.jeesite.modules.storage.service.RouteService;

/**
 * 栏目Controller
 */
@Controller(value = "webroute")
@RequestMapping(value = "${adminPath}/storage/route")
public class RouteController extends BaseController {

	@Autowired
	private RouteService routeService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Route get(String id, boolean isNewRecord) {
		return routeService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(Route route, Model model) {
		model.addAttribute("route", route);
		return "modules/storage/route/list";
	}

	/**
	 * 选择
	 */
	@RequestMapping(value = {"select", ""})
	public String select(Route route, Model model) {
		model.addAttribute("route", route);
		return "modules/storage/route/select";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Route> listData(Route route, HttpServletRequest request, HttpServletResponse response) {
		route.setPage(new Page<>(request, response));
		Page<Route> page = routeService.findPage(route);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(Route route, Model model) {
		model.addAttribute("route", route);
		return "modules/storage/route/form";
	}

	/**
	 * 保存栏目
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Route route) {
		try{
			routeService.save(route);
			return renderResult(Global.TRUE, text("保存栏目成功！"));
		}catch (Exception ex){
			return renderResult(Global.FALSE, text(ex.getMessage()));
		}
	}
	
	/**
	 * 删除栏目
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Route route) {
		try{
			routeService.delete(route);
			return renderResult(Global.TRUE, text("删除栏目成功！"));
		}catch (Exception ex){
			return renderResult(Global.FALSE, text(ex.getMessage()));
		}
	}
	
}